﻿using System.Collections.Generic;
using System.Linq;

using Google.Analytics.Common;
using Google.Analytics.Model.Feeds.Attributes;

namespace Google.Analytics.Model.Feeds
{
    public class AccountEntry : AnalyticsEntry
    {
        public AccountEntry() : base()
        {
            AddExtension(new Property());
            AddExtension(new TableId());
        }

        public string AccountID
        {
            get
            {
                Property property = Properties.Where(x => x.Name == "ga:accountId").FirstOrDefault();
                return (property == null) ? null : property.Value;
            }
        }

        public string AccountName
        {
            get
            {
                Property property = Properties.Where(x => x.Name == "ga:accountName").FirstOrDefault();
                return (property == null) ? null : property.Value;
            }
        }

        public string WebPropertyId
        {
            get
            {
                Property property = Properties.Where(x => x.Name == "ga:webPropertyId").FirstOrDefault();
                return (property == null) ? null : property.Value;
            }
        }

        public string Currency
        {
            get
            {
                Property property = Properties.Where(x => x.Name == "ga:currency").FirstOrDefault();
                return (property == null) ? null : property.Value;
            }
        }

        public string Timezone
        {
            get
            {
                Property property = Properties.Where(x => x.Name == "ga:timezone").FirstOrDefault();
                return (property == null) ? null : property.Value;
            }
        }

        /// <summary>
        /// This field controls the properties.
        /// </summary>
        public IList<Property> Properties
        {
            get
            {
                return FindExtensions<Property>(AnalyticsParserNameTable.XmlPropertyElement,
                             AnalyticsParserNameTable.AnalyticsNamespace);
            }
        }

        /// <summary>
        /// This field controls the tableId (ProfileId).
        /// </summary>
        public TableId ProfileId
        {
            get
            {
                return FindExtension(AnalyticsParserNameTable.XmlTableIdElement,
                                   AnalyticsParserNameTable.AnalyticsNamespace) as TableId;
            }
            set
            {
                ReplaceExtension(AnalyticsParserNameTable.XmlTableIdElement,
                                 AnalyticsParserNameTable.AnalyticsNamespace, value);
            }
        }
    }
}
